package com.example.sensors;

import java.util.Date;
import java.util.Map;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.provider.BaseColumns;
import android.provider.CallLog;

import com.example.singletones.SQLHelper;
import com.example.singletones.contextObj;

public class Sensor_Phone_Calls extends AbstractSensor {
	public static final String NAME = "PHONE_CALL";
	private static int mMaxID = -1;
	private final SQLHelper mSQLHelper;
	private SQLiteDatabase db = null;
	private final Handler mHandler = new Handler();

	public Sensor_Phone_Calls() {
		mSQLHelper = SQLHelper.getInstance();
		db = mSQLHelper.getWritableDatabase();
		Cursor cursor;
		cursor = db.rawQuery(
				"select data from `Configurations` where name='LastCallID'",
				null);
		while (cursor.moveToNext()) {
			if (cursor.getString(0) != null) {
				if (Integer.parseInt(cursor.getString(0)) > 0) {
					mMaxID = Integer.parseInt(cursor.getString(0));
				}
			}
		}
	}

	private final Runnable mUpdateTimeTask = new Runnable() {
		public void run() {
			getCallLog();
			mHandler.removeCallbacks(mUpdateTimeTask);
			mHandler.postDelayed(mUpdateTimeTask, 10000);
		}
	};

	@Override
	public void disableSensor() {
		super.disableSensor();
		mHandler.removeCallbacks(mUpdateTimeTask);
	}

	@Override
	public void enableSensor() {
		super.enableSensor();
		mHandler.postDelayed(mUpdateTimeTask, 20000);
	}

	public void getCallLog() {
		// StringBuffer sb = new StringBuffer();
		Cursor managedCursor = contextObj
				.getInstance()
				.getmContext()
				.getContentResolver()
				.query(android.provider.CallLog.Calls.CONTENT_URI, null,
						"_id > ?", new String[] { String.valueOf(mMaxID) },
						null);
		int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
		int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
		int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
		int id = managedCursor.getColumnIndex(BaseColumns._ID);
		while (managedCursor.moveToNext()) {
			String phNumber = managedCursor.getString(number);
			String callType = managedCursor.getString(type);
			String callDate = managedCursor.getString(date);
			new Date(Long.valueOf(callDate));
			String _id = managedCursor.getString(id);
			String dir = null;
			int dircode = Integer.parseInt(callType);
			switch (dircode) {
			case CallLog.Calls.OUTGOING_TYPE:
				dir = "OUTGOING";
				break;

			case CallLog.Calls.INCOMING_TYPE:
				dir = "INCOMING";
				break;

			case CallLog.Calls.MISSED_TYPE:
				dir = "MISSED";
				break;
			}

			if (Integer.parseInt(_id) > mMaxID && phNumber.length() > 5) {
				mMaxID = Integer.parseInt(_id);
				Map<String, Object> eventArgs = initEventArgs();
				eventArgs.put("eventType", "sensorInfoReceived");
				eventArgs.put("phoneNumber", phNumber);
				eventArgs.put("callDir", dir);
				OnSensorChanged(eventArgs);
			}
		}
		managedCursor.close();
		mSQLHelper.insertConfigData("lastCallID", "" + mMaxID);
	}

	public String getSensorName() {
		return NAME;
	}

}
